<?php

/**
 * @author blog.anchen8.net
 * @copyright 2015
 */
class financial extends actionAbstract{
    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
        $this->partnerInfo = $this->userinfo=acl::checkLogin('PmsCookie','/index.php/partner/common/login');
        if($this->partnerInfo['parentid']!=0){
        	$this->partnerInfo['id'] = $this->partnerInfo['parentid'];
        }
    }
    function hotelziduan($ziduan){
        $this->loadModel('hotel','category');
        $re=$this->hotel->categoryModel->fetchRow("select $ziduan from hotel_category where id=".(isset($_SESSION['hotel_id'])?$_SESSION['hotel_id']:0)." and partnerid=".$this->partnerInfo['id']);
    	if($re){
        	return $re[$ziduan];
        }else{
        	return 0;
        }
    }
    
    //报表统计
    function statistics(){
        $leftpost="financial";
        $leftbot="statistics";
        global $pmsconfigs;
    	$this->loadModel('hotel','type');
    	$this->loadModel('hotel','category');
    	$this->loadModel('pms','orders');
    	$this->loadModel('pms','financial');
    	$this->loadModel('pms','channels');
        $this->loadHelper('pager');
    	$hotelId=$this->hotelziduan('id');
    	$hotelName=$this->hotelziduan('hotelName');
    	
        $qudaos=$this->pms->channelsModel->fetchAll("select * from pms_channels where id>0 order by sort asc");
        
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $orderStatus=isset($_GET['orderStatus'])?$_GET['orderStatus']:9999;
        $qudao=isset($_GET['qudao'])?$_GET['qudao']:9999;
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        
        $where="(a.status=1 or a.status=3 or a.status=4) and a.hotelId=$hotelId";
        if($sta){
            $where.=" and a.endDate>='".strtotime($sta)."'";
        }
        if($end){
            $where.=" and a.endDate<='".strtotime($end)."'";
        }
        if($orderStatus&&$orderStatus!=9999){
            $where.=" and a.status='".$orderStatus."'";
        }
        if($qudao!=9999){
            $where.=" and a.type='".$qudao."'";
        }
        
        $size = 10;
        $offset = ($page - 1) * $size;
        
    	$sql="select a.*,b.hotelName,c.title,d.status as finStatus,e.title as qudaoName from pms_orders as a left join hotel_category as b on a.hotelId=b.id";
        $sql.=" left join hotel_type as c on c.id=a.roomId left join pms_financial as d on d.orderKey=a.id left join pms_channels as e";
        $sql.=" on e.osiiId=a.type where ".$where." order by a.created desc";
        
    	$reto=$this->pms->ordersModel->fetchAll($sql);
        $countPrice=0;
        if($reto){
            foreach($reto as $k=>$v){
                $countPrice=$countPrice+$v['price'];
            }
        }
        
        $count=count($reto);
        $number = ceil( $count / $size );
        $extend = new pager();
        $pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
        //数组分页
        $str=($page-1)*$size;
        $re=array_slice($reto,$str,$size);
        $pagePrice=0;
        if($re){
            foreach($re as $k=>$v){
                $pagePrice=$pagePrice+$v['price'];
            }
        }
        
        include $this->loadWidget('partnerTheme');
    }
    
    //导出报表统计
    public function export(){
    	$this->loadModel('hotel','type');
    	$this->loadModel('hotel','category');
    	$this->loadModel('pms','orders');
    	$this->loadModel('pms','financial');
    	$this->loadModel('pms','channels');
    	$hotelId=$this->hotelziduan('id');
    	$hotelName=$this->hotelziduan('hotelName');
    	
        $where="a.hotelId=$hotelId";
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $orderStatus=isset($_GET['orderStatus'])?(int)($_GET['orderStatus']):9999;
        $qudao=isset($_GET['qudao'])?(int)($_GET['qudao']):9999;
        
        if($sta){
            $where.=" and a.endDate>='".strtotime($sta)."'";
        }
        if($end){
            $where.=" and a.endDate<='".strtotime($end)."'";
        }
        if($orderStatus&&$orderStatus!=9999){
            $where.=" and a.status='".$orderStatus."'";
        }else{
            $where.=" and (a.status=1 or a.status=3 or a.status=4)";
        }
        if($qudao!=9999){
            $where.=" and a.type='".$qudao."'";
        }
        
    	$sql="select a.*,b.hotelName,c.title,d.status as finStatus,e.title as qudaoName from pms_orders as a left join hotel_category as b on a.hotelId=b.id";
    	$sql.=" left join hotel_type as c on c.id=a.roomId left join pms_financial as d on d.orderKey=a.id left join pms_channels as e";
    	$sql.=" on e.osiiId=a.type where ".$where." order by a.created desc";
    	
    	$re=$this->pms->ordersModel->fetchAll($sql);
        $title=$hotelName."-报表统计".date("Y-m-d",time());
    	$filed=array('订单号','预订人','预订者手机','房型','间数','入住日期','离店日期','订单状态','收款状态','订单来源','金额');
    	 
    	$result = array();
    	foreach($re as $k=>$val){
    		$result[$k]['orderId']	=	$val['orderId'];
    		$result[$k]['name']		=	$val['name'];
    		$result[$k]['mobile']	=	$val['mobile'];
    		$result[$k]['title']	=	$val['title'];
    		$result[$k]['countRoom']=	$val['countRoom'];
    		$result[$k]['startDate']=	date("Y-m-d",$val['startDate']);
    		$result[$k]['endDate']	=	date("Y-m-d",$val['endDate']);
            
            $statusName="";
            if($val['status']==1){
                $statusName="待入住";
            }elseif($val['status']==3){
                $statusName="已完成";
            }elseif($val['status']==4){
                $statusName="已入住";
            }
    		$result[$k]['status']   =   $statusName;
    		$result[$k]['finStatus']  =   $val['finStatus']==1?'已结算':'未结算';
    		$result[$k]['qudaoName'] =   empty($val['qudaoName'])?'线下':$val['qudaoName'];
    		$result[$k]['price']	=	$val['price'];
    	}
    
    	$this->outExcel($title,$filed,$result);
    }
     /**
     * 结算明细
     */
    function clearanceDetailed(){
        $leftpost="financial";
        $leftbot="clearanceDetailed";
        $this->loadModel('hotel','type');
        $this->loadModel('hotel','category');
        $this->loadModel('pms','orders');
        $this->loadModel('pms','financial');
        $this->loadModel('pms','channels');
        $this->loadHelper('pager');
        $hotelId=$this->hotelziduan('id');
        global $pmsconfigs;
        
        $qudaos=$this->pms->channelsModel->fetchAll("select * from pms_channels where id>0 order by sort asc");
        
        $orderId=isset($_GET['orderId'])?$_GET['orderId']:'';
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $qudao=isset($_GET['qudao'])?$_GET['qudao']:9999;
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        
        $where="(a.status=1 or a.status=3 or a.status=4) and a.hotelId=$hotelId";
        if($orderId){
            $where.=" and a.orderId like '%".$orderId."%'";
        }
        if($sta){
            $where.=" and a.endDate>='".strtotime($sta)."'";
        }
        if($end){
            $where.=" and a.endDate<='".strtotime($end)."'";
        }
        if($qudao!=9999){
            $where.=" and a.type='".$qudao."'";
        }
        
        $size = 10;
        $offset = ($page - 1) * $size;
        
        $sql="select a.*,b.hotelName,c.title,d.status as finStatus,e.title as qudaoName from pms_orders as a left join hotel_category as b on a.hotelId=b.id";
        $sql.=" left join hotel_type as c on c.id=a.roomId left join pms_financial as d on d.orderKey=a.id left join pms_channels as e";
        $sql.=" on e.osiiId=a.type where ".$where." order by a.created desc";
        
        $reto=$this->pms->ordersModel->fetchAll($sql);
        $countPrice=0;
        if($reto){
            foreach($reto as $k=>$v){
                $countPrice=$countPrice+$v['price'];
            }
        }
        
        $count=count($reto);
        $number = ceil( $count / $size );
        $extend = new pager();
        $pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
        //数组分页
        $str=($page-1)*$size;
        $re=array_slice($reto,$str,$size);
        $pagePrice=0;
        if($re){
            foreach($re as $k=>$v){
                $pagePrice=$pagePrice+$v['price'];
            }
        }
        
        include $this->loadWidget('partnerTheme');
    }
    
    /**
     * 导出结算明细
     */
    function exportClearance(){
        $this->loadModel('hotel','type');
        $this->loadModel('hotel','category');
        $this->loadModel('pms','orders');
        $this->loadModel('pms','financial');
        $this->loadModel('pms','channels');
        $hotelId=$this->hotelziduan('id');
        $hotelName=$this->hotelziduan('hotelName');
        
        $where="";
        $orderId=isset($_GET['orderId'])?$_GET['orderId']:'';
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $qudao=isset($_GET['qudao'])?(int)($_GET['qudao']):9999;
        
        if($orderId){
            $where.=" and a.orderId like '%".$orderId."%'";
        }
        if($sta){
            $where.=" and a.endDate>='".strtotime($sta)."'";
        }
        if($end){
            $where.=" and a.endDate<='".strtotime($end)."'";
        }
        if($qudao!=9999){
            $where.=" and a.type='".$qudao."'";
        }
        
        $sql="select a.*,b.hotelName,c.title,d.status as finStatus,e.title as qudaoName from pms_orders as a left join hotel_category as b on a.hotelId=b.id";
        $sql.=" left join hotel_type as c on c.id=a.roomId left join pms_financial as d on d.orderKey=a.id left join pms_channels as e";
        $sql.=" on e.osiiId=a.type where (a.status=1 or a.status=4) and a.hotelId=$hotelId".$where." order by a.created desc";
        
        $re=$this->pms->ordersModel->fetchAll($sql);
        
        $title=$hotelName."-结算明细".date("Y-m-d_H-i-s",time());
        $filed=array('序号','订单号','结算状态','渠道','订单金额','入住时间','离店时间');
        $result = array();
		foreach($re as $k=>$val){
            $result[$k]['id']	      =	$k+1;
            $result[$k]['orderId']	  =	$val['orderId'];
            $result[$k]['finStatus']  =	$val['finStatus']==1?'已结算':'未结算';
            $result[$k]['qudaoName']  =	$val['qudaoName'];
            $result[$k]['price']      =	$val['price'];
            $result[$k]['startDate']  =	date('Y-m-d',$val['startDate']);
            $result[$k]['endDate']	  =	date('Y-m-d',$val['endDate']);
		}
        $this->outExcel($title,$filed,$result);
    }


    /**
     * 结算账户
     */
    function clearance(){
        $leftpost="financial";
        $leftbot="clearance";
        $pmsId=$this->partnerInfo['id'];
        $this->loadModel('pms','bank');
        $re=$this->pms->bankModel->fetchRow("select * from pms_bank where pmsId=".$pmsId);
        
        include $this->loadWidget('partnerTheme');
    }
    //添加、修改结算账户
    function clearanceEdit(){
        $pmsId=$this->partnerInfo['id'];
        $this->loadModel('pms','bank');
        $this->loadHelper('extend');
        global $pmsconfigs;
        
        $data=isset($_POST['data'])?$_POST['data']:'';
        if(trim($data['bankName'])==''){
            ajaxReturn('', '请填写开户行！', 0);
        }elseif(trim($data['accountName'])==''){
            ajaxReturn('', '请填写账户名称！', 0);
        }elseif(trim($data['bankaccount'])==''){
            ajaxReturn('', '请填写银行账号！', 0);
        }elseif(strlen($data['phone'])!=11||!in_array(substr($data['phone'],0,3),$pmsconfigs['phone'])){
            ajaxReturn('', '请填写正确的手机号！', 0);
        }
        $data['created']=date('Y-m-d H:i:s',time());
        $data['status']=0;
        $data['pmsId']=$pmsId;
        
        $th_re=$this->pms->bankModel->fetchRow("select id from pms_bank where pmsId=".$pmsId);
        if($th_re){
            $re=$this->pms->bankModel->update($data,"id=".$th_re['id']." and pmsId=".$pmsId);
        }else{
            $re=$this->pms->bankModel->insert($data);
        }
        if($re){
            ajaxReturn('refurbish', '操作成功！', 1);
        }
        ajaxReturn('', '操作失败！', 0);
    }
    
    //应收账务
    function receivable(){
        $leftpost="financial";
        $leftbot="receivable";
        $this->loadModel('hotel','type');
        $this->loadModel('hotel','category');
        $this->loadModel('pms','orders');
        $this->loadModel('pms','financial');
        $this->loadModel('pms','channels');
        $this->loadHelper('pager');
        $hotelId=$this->hotelziduan('id');
        global $pmsconfigs;
        
        $qudaos=$this->pms->channelsModel->fetchAll("select * from pms_channels where id>0 order by sort asc");
        
        $orderId=isset($_GET['orderId'])?$_GET['orderId']:'';
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $qudao=isset($_GET['qudao'])?$_GET['qudao']:'9999';
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        
        $where="(a.status=1 or a.status=3 or a.status=4) and a.hotelId=$hotelId";
        if($orderId){
            $where.=" and a.orderId like '%".$orderId."%'";
        }
        if($sta){
            $where.=" and a.endDate>='".strtotime($sta)."'";
        }
        if($end){
            $where.=" and a.endDate<='".strtotime($end)."'";
        }
        if($qudao!='9999'){
            $where.=" and a.type='".$qudao."'";
        }
        
        $size = 10;
        $offset = ($page - 1) * $size;
        
        $sql="select a.*,b.hotelName,c.title,d.status as finStatus,e.title as qudaoName from pms_orders as a left join hotel_category as b on a.hotelId=b.id";
        $sql.=" left join hotel_type as c on c.id=a.roomId left join pms_financial as d on d.orderKey=a.id left join pms_channels as e";
        $sql.=" on e.osiiId=a.type where ".$where." order by a.created desc";
        
        $reto=$this->pms->ordersModel->fetchAll($sql);
        $countPrice=0;
        if($reto){
            foreach($reto as $k=>$v){
                if($v['finStatus']==1){
                    unset($reto[$k]);
                }else{
                    $countPrice=$countPrice+$v['price'];
                }
            }
        }
        
        $count=count($reto);
        $number = ceil( $count / $size );
        $extend = new pager();
        $pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
        //数组分页
        $str=($page-1)*$size;
        $re=array_slice($reto,$str,$size);
        
        $pagePrice=0;
        if($re){
            foreach($re as $k=>$v){
                $pagePrice=$pagePrice+$v['price'];
            }
        }
        
        include $this->loadWidget('partnerTheme');
    }
    //确认收款
    function editReceivable(){
        $this->loadModel('pms','financial');
        $id=isset($_POST['id'])?(int)($_POST['id']):0;
        
        $th=$this->pms->financialModel->fetchRow("select id from pms_financial where orderKey=$id");
        $data['status']=1;
        if($th){
            $re=$this->pms->financialModel->update($data,"id=".$th['id']);
        }else{
            $data['orderKey']=$id;
            $re=$this->pms->financialModel->insert($data);
        }
        if($re){
            exit(json_encode(array('state'=>1,'html'=>'操作成功！')));
        }
        exit(json_encode(array('state'=>0,'html'=>'操作失败！')));
    }
    //导出应收账务
    function exportReceivable(){
        $this->loadModel('hotel','type');
        $this->loadModel('hotel','category');
        $this->loadModel('pms','orders');
        $this->loadModel('pms','financial');
        $this->loadModel('pms','channels');
        $hotelId=$this->hotelziduan('id');
        $hotelName=$this->hotelziduan('hotelName');
        
        $where="";
        $orderId=isset($_GET['orderId'])?$_GET['orderId']:'';
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $qudao=isset($_GET['qudao'])?(int)($_GET['qudao']):9999;
        
        if($orderId){
            $where.=" and a.orderId like '%".$orderId."%'";
        }
        if($sta){
            $where.=" and a.endDate>='".strtotime($sta)."'";
        }
        if($end){
            $where.=" and a.endDate<='".strtotime($end)."'";
        }
        if($qudao!=9999){
            $where.=" and a.type='".$qudao."'";
        }
        
        $sql="select a.*,b.hotelName,c.title,d.status as finStatus,e.title as qudaoName from pms_orders as a left join hotel_category as b on a.hotelId=b.id";
        $sql.=" left join hotel_type as c on c.id=a.roomId left join pms_financial as d on d.orderKey=a.id left join pms_channels as e";
        $sql.=" on e.osiiId=a.type where (a.status=1 or a.status=4) and a.hotelId=$hotelId".$where." order by a.created desc";
        
        $re=$this->pms->ordersModel->fetchAll($sql);
        if($re){
            foreach($re as $k=>$v){
                if($v['finStatus']==1){
                    unset($re[$k]);
                }
            }
        }
        
        $title=$hotelName."-应收报表".date("Y-m-d_H-i-s",time());
        $filed=array('序号','订单号','预订人','房型','间数','入住时间','离店时间','入住状态','渠道','金额');
        $result = array();
		foreach($re as $k=>$val){
            $result[$k]['id']	      =	$k+1;
            $result[$k]['orderId']	  =	$val['orderId'];
            $result[$k]['name']	      =	$val['name'];
            $result[$k]['title']	  =	$val['title'];
            $result[$k]['countRoom']  =	$val['countRoom'];
            $result[$k]['startDate']  =	date('Y-m-d',$val['startDate']);
            $result[$k]['endDate']	  =	date('Y-m-d',$val['endDate']);
            
            $statusName="";
            if($val['status']==1){
                $statusName="待入住";
            }elseif($val['status']==3){
                $statusName="已完成";
            }elseif($val['status']==4){
                $statusName="已入住";
            }
            $result[$k]['status']	  =	$statusName;
            $result[$k]['qudaoName']  =	$val['qudaoName'];
            $result[$k]['price']      =	$val['price'];
		}
        $this->outExcel($title,$filed,$result);
    }
}